//
//  HomeGuideViewController.m
//  CedarFinanceC
//
//  Created by lshenrong on 2017/11/27.
//  Copyright © 2017年 cedarhd. All rights reserved.
//

#import "HomeGuideViewController.h"
#import "UIButton+ButtonStyle.h"

@interface GuideViewCell : UICollectionViewCell

@property (nonatomic, strong) UIImageView *imageView;
@property (nonatomic, strong) CedarButton *button;

@end

static NSString *const identifier = @"GuideCell";

@implementation GuideViewCell

- (instancetype)init {
    self = [super init];
    if (self) {
        [self initialization];
    }
    return self;
}

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self initialization];
    }
    return self;
}

- (void)initialization {
    self.layer.masksToBounds = YES;
    self.imageView = [[UIImageView alloc] init];
    self.imageView.frame = SCREEN_BOUNDS;
    self.imageView.center = CGPointMake(SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2);
    CedarButton *button = [CedarButton buttonWithRegisterText:@"立即体验"];
    button.hidden = YES;
    [button setFrame:CGRectMake(0, 0, 160, 40)];
    self.button = button;
    [self.contentView addSubview:self.imageView];
    [self.contentView addSubview:self.button];
    [self.button setCenter:CGPointMake(SCREEN_WIDTH / 2, SCREEN_HEIGHT - 90)];
}

@end





@interface HomeGuideViewController()<UIScrollViewDelegate,UICollectionViewDelegate, UICollectionViewDataSource>

@property (nonatomic,strong)  UICollectionView *collectionView;
@property (nonatomic,strong)  NSMutableArray   *images;
@property (nonatomic,strong)  dispatch_source_t timer;
@property (nonatomic,assign)  NSInteger currentIndex;


@end

@implementation HomeGuideViewController

- (void)viewDidLoad{
    self.currentIndex = 0;
    self.images = [NSMutableArray array];
    for (int i = 1; i <= 3; i++) {//AppGuid1
        NSString *imageName = [NSString stringWithFormat:@"AppGuid%d",i];
        [self.images addObject:imageName];
    }
    [self.view addSubview:self.collectionView];
    [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(self.view);
        make.bottom.mas_equalTo(self.view);
        make.top.mas_equalTo(self.view);
    }];
    [self.collectionView reloadData];
    
   // [self setGuideTimer];
    
}

- (void)setGuideTimer{
    //获得队列
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    //创建一个定时器
    self.timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue);
    //设置开始时间
    dispatch_time_t start = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3.0 * NSEC_PER_SEC));
    //设置时间间隔
    uint64_t interval = (uint64_t)(2.0* NSEC_PER_SEC);
    //设置定时器
    dispatch_source_set_timer(self.timer, start, interval, 0);
    //设置回调
    dispatch_source_set_event_handler(self.timer, ^{
        //设置当执行五次是取消定时器
       // count++;
        //回主线刷新UI
        if (self.currentIndex == [self.images count]-1) {
            dispatch_source_cancel(self.timer);
            self.currentIndex = 0;
            return ;
        }
        self.currentIndex++;
        dispatch_async(dispatch_get_main_queue(), ^{
            NSIndexPath *indexPath = [NSIndexPath indexPathForRow:self.currentIndex inSection:0];
            [self.collectionView scrollToItemAtIndexPath:indexPath atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally animated:YES];
        });
    });
    //启动定时器
    dispatch_resume(self.timer);
}

- (void)pauseTimer{
    if(_timer){
        dispatch_suspend(_timer);
    }
}

- (void) resumeTimer{
    if(_timer){
        dispatch_resume(_timer);
    }
}

-(void) stopTimer{
    if(_timer){
        dispatch_source_cancel(_timer);
        _timer = nil;
    }
}



#pragma mark - 引导页代码

- (UICollectionView *)collectionView {
    if (_collectionView == nil) {
        CGRect screen = [UIScreen mainScreen].bounds;
        UICollectionViewFlowLayout *layout = [UICollectionViewFlowLayout new];
        layout.minimumInteritemSpacing = 0;
        layout.minimumLineSpacing = 0;
        layout.itemSize = screen.size;
        layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        _collectionView = [[UICollectionView alloc] initWithFrame:screen collectionViewLayout:layout];
        _collectionView.bounces = NO;
        _collectionView.backgroundColor = [UIColor whiteColor];
        _collectionView.showsHorizontalScrollIndicator = NO;
        _collectionView.showsVerticalScrollIndicator = NO;
        _collectionView.pagingEnabled = YES;
        _collectionView.dataSource = self;
        _collectionView.delegate = self;
        [_collectionView registerClass:[GuideViewCell class] forCellWithReuseIdentifier:identifier];
    }
    return _collectionView;
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return [self.images count];
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    GuideViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];
    NSString *imageName = [self.images objectAtIndex:indexPath.row];
    UIImage *img = [UIImage imageNamed:imageName];
    if (!img) {
        return cell;
    }
    //CGSize size = [self adapterSizeImageSize:img.size compareSize:SCREEN_BOUNDS.size];
    cell.imageView.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
    cell.imageView.image = img;
    cell.imageView.center = CGPointMake(SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2);
    if (indexPath.row == self.images.count - 1) {
        UITapGestureRecognizer *tapGes = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(nextAction:)];
        [cell.imageView setUserInteractionEnabled:YES];
        [cell.imageView addGestureRecognizer:tapGes];
        NSMutableDictionary* p = [[NSMutableDictionary alloc] init];
        [p setValue:@"进入大金所" forKey:@"$element_content"];
        [cell.button setHidden:YES];
//        [cell.button addTarget:self action:@selector(nextAction:) forControlEvents:UIControlEventTouchUpInside];
    } else {
        [cell.button setHidden:YES];
    }
    return cell;
}

- (CGSize)adapterSizeImageSize:(CGSize)is compareSize:(CGSize)cs{
    CGFloat w = cs.width;
    CGFloat h = cs.width / is.width * is.height;
    if (h < cs.height) {
        w = cs.height / h * w;
        h = cs.height;
    }
    return CGSizeMake(w, h);
}

- (void)nextAction:(UIButton *)button{
   
    if(self.finishBlock){
        self.finishBlock();
    }
}


@end





